package com.xinjian.jianba.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xinjian.jianba.dao.dwmid.LableTreeDao;
import com.xinjian.jianba.dao.dwmid.UserLableDao;
import com.xinjian.jianba.dto.LableTreeDto;
import com.xinjian.jianba.enity.LableTreeEnity;
import com.xinjian.jianba.enity.UserLableEnity;
import com.xinjian.jianba.service.LableService;
import com.xinjian.jianba.service.RedisService;
import com.xinjian.jianba.utils.JsonUtil;

@Service
public class LableServiceImpl implements LableService {

	private static final Logger LOGGER = LoggerFactory.getLogger(LableService.class);

	@Value("${lable.icon}")
	private String lableIcon;

	@Value("${spring.meet.label.db}")
	private String labelDb;

	@Autowired
	private LableTreeDao lableTreeDao;

	@Autowired
	private UserLableDao userLableDao;

	@Autowired
	private RedisService redisService;

	/**
	 * 转换为树形结构
	 * 
	 * @return
	 * @throws Exception 
	 *
	 */
	@Override
	public List<LableTreeDto> LableTree() throws Exception {
		List<LableTreeDto> resultList = new ArrayList<LableTreeDto>();

		String labelString = redisService.get(labelDb);
		if (StringUtils.isNotBlank(labelString)) {
			resultList = JsonUtil.readJson2Array(labelString, LableTreeDto.class);
		} else {

			Map<String, String> idAndNameMap = new HashMap<String, String>();
			Map<String, LableTreeEnity> lableInfo = new HashMap<String, LableTreeEnity>();

			Map<String, List<String>> rootMap = new LinkedHashMap<String, List<String>>();
			List<LableTreeEnity> labletreeList = lableTreeDao.selectAll();
			if (!CollectionUtils.isEmpty(labletreeList)) {
				for (LableTreeEnity lableTree : labletreeList) {
					idAndNameMap.put(lableTree.getLable1Id(), lableTree.getLable1Name());
					idAndNameMap.put(lableTree.getLable2Id(), lableTree.getLable2Name());
					lableInfo.put(lableTree.getLable1Id(), lableTree);
					if (rootMap.containsKey(lableTree.getLable1Id())) {
						List<String> secondList = rootMap.get(lableTree.getLable1Id());
						secondList.add(lableTree.getLable2Id());
						rootMap.put(lableTree.getLable1Id(), secondList);

					} else {
						List<String> secondList = new ArrayList<String>();
						secondList.add(lableTree.getLable2Id());
						rootMap.put(lableTree.getLable1Id(), secondList);

					}
				}

			}

			Iterator<Entry<String, List<String>>> it = rootMap.entrySet().iterator();
			while (it.hasNext()) {
				Entry<String, List<String>> entity = (Entry<String, List<String>>) it.next();
				LableTreeDto rootDto = new LableTreeDto(entity.getKey(), idAndNameMap.get(entity.getKey()));
				LableTreeEnity lableTree = lableInfo.get(entity.getKey());
				if (lableTree != null) {
					rootDto.setLableColor(lableTree.getLable1Color());
					rootDto.setLableIcon(lableIcon.concat(lableTree.getLable1Icon()));
				}
				List<String> secondChild = entity.getValue();

				if (!CollectionUtils.isEmpty(secondChild)) {
					List<LableTreeDto> secondChildenList = new ArrayList<LableTreeDto>();
					for (String id : secondChild) {
						LableTreeDto thirdDto = new LableTreeDto(id, idAndNameMap.get(id));
						secondChildenList.add(thirdDto);
					}
					rootDto.setChild(secondChildenList);
				}

				resultList.add(rootDto);
			}

			redisService.set(labelDb, JsonUtil.getJsonString(resultList));
			redisService.expire(labelDb, 24 * 60 * 60);
		}
		return resultList;
	}

	/**
	 * 用户标签树
	 * 
	 * @author liuhaitao
	 * @param userId
	 * @return
	 *
	 */
	@Override
	public List<LableTreeDto> UserlableTree(Long userId) {
		List<UserLableEnity> userLableList = userLableDao.selectByUserId(userId);
		return userLableTree(userLableList);
	}

	/**
	 * 用户标签树
	 */
	private List<LableTreeDto> userLableTree(List<UserLableEnity> userLableList) {
		Map<String, LableTreeEnity> lableInfo = new HashMap<String, LableTreeEnity>();

		List<LableTreeEnity> labletreeList = lableTreeDao.selectAll();
		if (!CollectionUtils.isEmpty(labletreeList)) {
			for (LableTreeEnity lableTree : labletreeList) {
				lableInfo.put(lableTree.getLable1Id(), lableTree);
			}
		}

		Map<String, String> idAndNameMap = new HashMap<String, String>();

		Map<String, List<String>> rootMap = new LinkedHashMap<String, List<String>>();

		if (!CollectionUtils.isEmpty(userLableList)) {
			for (UserLableEnity lableTree : userLableList) {
				idAndNameMap.put(lableTree.getLable1Id(), lableTree.getLable1Name());
				idAndNameMap.put(lableTree.getLable2Id(), lableTree.getLable2Name());
				if (rootMap.containsKey(lableTree.getLable1Id())) {
					List<String> secondList = rootMap.get(lableTree.getLable1Id());
					secondList.add(lableTree.getLable2Id());
					rootMap.put(lableTree.getLable1Id(), secondList);

				} else {
					List<String> secondList = new ArrayList<String>();
					secondList.add(lableTree.getLable2Id());
					rootMap.put(lableTree.getLable1Id(), secondList);

				}
			}

		}

		LOGGER.info("----------rootMap-------------" + JsonUtil.getJsonString(rootMap));

		Iterator<Entry<String, List<String>>> it = rootMap.entrySet().iterator();
		List<LableTreeDto> resultList = new ArrayList<LableTreeDto>();
		while (it.hasNext()) {
			Entry<String, List<String>> entity = (Entry<String, List<String>>) it.next();
			LableTreeDto rootDto = new LableTreeDto(entity.getKey(), idAndNameMap.get(entity.getKey()));
			LableTreeEnity lableTree = lableInfo.get(entity.getKey());
			if (lableTree != null) {
				rootDto.setLableColor(lableTree.getLable1Color());
				rootDto.setLableIcon(lableIcon.concat(lableTree.getLable1Icon()));
			}
			List<String> secondChild = entity.getValue();

			if (!CollectionUtils.isEmpty(secondChild)) {
				List<LableTreeDto> secondChildenList = new ArrayList<LableTreeDto>();
				for (String id : secondChild) {
					LableTreeDto thirdDto = new LableTreeDto(id, idAndNameMap.get(id));
					secondChildenList.add(thirdDto);
				}
				rootDto.setChild(secondChildenList);
			}

			resultList.add(rootDto);
		}

		return resultList;

	}
}
